<?php
class Order_OrderController extends Zend_Controller_Action
{
	/**
	* 订单列表   
	*
	*/
	private $_s;
	public function preDispatch(){
		$this->_s=new Order_Service_Order();
		$this->tplDirectory = "order/order/";	//控制对应模板目录
// 		print_r($_COOKIE);
	}	
	
	public function indexAction(){
		$value = '1';
		$field = 'st_status';
		$shipType = Shipping_Service_ShipType::getAllByValue($value,$field);
		$shipTypeArr = array();
		foreach($shipType as $v){
			$shipTypeArr[$v['st_code']] = $v;
		}
		ksort($shipTypeArr);
		
		$processStatus = Order_Service_Order::getAllProcessStatus();
		
		$pickusers= User_Service_User::getByfield('ud_id', 12);
		
		$this->view->shipType = $shipTypeArr;
		$this->view->processStatus = $processStatus;
		$this->view->pickusers = $pickusers;
		
		echo Cff::renderTpl($this->tplDirectory.'index.tpl',"layout");
	}
	
	public function detailAction(){
		$orders_code=$this->_request->getParam('orders_code','');
		$this->view->orders_code=$orders_code;
		echo Cff::renderTpl($this->tplDirectory.'detail.tpl',"layoutheader");
	}
	/**
	*返回订单产品详细
	*Param op_id
	*/
	public function prodetailAction(){
		$op_id=$this->_request->getParam('id','');
		if(!empty($op_id)){
			$opRows=Order_Service_Product::getById($op_id);
		}
		$this->view->op_id=$op_id;
		$this->view->orders_code=$opRows['orders_code'];
		$this->view->product_id=$opRows['product_id'];
		$this->view->op_declared_value=$opRows['op_declared_value'];
		$this->view->product_sku=$opRows['product_sku'];
		$this->view->op_quantity=$opRows['op_quantity'];
		$this->view->op_title=$opRows['op_title'];
		$this->view->op_description=$opRows['op_description'];
		echo Cff::renderTpl($this->tplDirectory.'prodetail.tpl',"layoutheader");
	}

	/**
	*产品列表页面
	*Param orders_code
	*/
	public function productAction(){
		$orders_code=$this->_request->getParam('orders_code','');
		$rows=$this->_s->getByOrdersCode($orders_code);
		$this->view->customer_id=$rows['customer_id'];
		$this->view->orders_code=$orders_code;
		echo Cff::renderTpl($this->tplDirectory.'productlist.tpl',"layoutheader");
	}

	/**
	*根据用户ID返回产品列表
	*Param customer_id
	*retrun json
	*/
	public function productlistAction(){
		$intpage=$this->_request->getParam('page',1);
		$pageSize=$this->_request->getParam('pageSize');
		$customer_id=$this->_request->getParam('customer_id','');
		$product=$this->_request->getParam('product_id','');//ID或名称
		$intpage = ($intpage<1)?1:$intpage; 
		$condition=array(
			'product'=>$product,
			'customer_id'=>$customer_id,
			'product_status'=>1,//激活状态
		);
		$count=Product_Service_Product::getByCondition($condition,'count(*)','',0,0);
		$data=array(
			'count'=>0,
			'ask'=>0
			);
		if($count>0){
			$Rows=Product_Service_Product::getByCondition($condition,'*',array('product_id desc'),$pageSize,$intpage);
			foreach($Rows as $key => $value){
				$products=Warehouse_Service_Products::getByProductId($value['product_id']);
				$Rows[$key]['ws_code']=$products['ws_code'];
				$Rows[$key]['wp_quantity']=$products['wp_quantity'];
			}
			$data=array(
				'count'=>$count,
				'result'=>$Rows,
				'ask'=>1
			);
		}
		die(Zend_Json::encode($data));
	}
	
	public function listAction(){
		$intpage=$this->_request->getParam('page',1);
		$pageSize=$this->_request->getParam('pageSize');
		
		$orders_status=$this->_request->getParam('orders_status_sch','');	//搜索表单提交的状态
		$customerId=$this->_request->getParam('customerId','');	//搜索表单提交的客户代码
		$customerId = trim($customerId);
		$searchType = $this->_request->getParam('searchType','orders_code');//代码类型
		$code = $this->_request->getParam('code','');	//搜索表单提交的订单代码		
		
		$orders_print_type = $this->_request->getParam('orders_print_type',"-1");//订单类型
		
		$dateType = $this->_request->getParam('dateType','add_time');//代码类型
		$dateFrom = $this->_request->getParam('dateFrom','');//代码类型
		$dateTo = $this->_request->getParam('dateTo','');//代码类型
		$pickusersId=$this->_request->getParam('pickusers',''); //配货员
		
		
		
		$code = trim($code);
		$orders_code_list = array();
		if($code){				
			if($searchType=="pickup_code"){				
				
				$model_orderPickupMap = new Order_Model_OrderPickupMap();
				$orderPickupMaps = $model_orderPickupMap->getByOpCode($code);
				if(is_array($orderPickupMaps)){
					foreach($orderPickupMaps as $v){
						$orders_code_list[] = $v['orders_code'];				
					}		
				}
			}elseif($searchType=="ship_code"){		
				$model_orderShip = new Order_Model_OrderShip();
				$orderShip = $model_orderShip->getByTrackingNumber($code);
				if(is_array($orderShip)){					
					foreach($orderShip as $v){
						$orders_code_list[] = $v['orders_code'];
					}	
				}	
				
			}elseif($searchType=="ref_code"){		
				$orders =  Order_Service_Order::getByfield("customer_order_code",$code);
				if(is_array($orders)){					
					foreach($orders as $v){
						$orders_code_list[] = $v['orders_code'];
					}	
				}	
				
			}else{
				$orders_code_list[] = $code;
			}
			
			if(empty($orders_code_list)){
				$orders_code_list[] = md5(time());//随机数
			}
			
		}
		
		
//		print_r($orders_code_list);die();
		$payment_status=$this->_request->getParam('payment_status','');
		$warehouse_id=$this->_request->getParam('warehouse_id','');
		$shipping_method = $this->_request->getParam('shipping_method','');
		
		$browsetype=$this->_request->getParam('browsetype','fold');//收展产品

		$intpage = ($intpage<1)?1:$intpage; 
		$condition=array(
			'orders_code'=>$orders_code_list,
			'process_status'=>$orders_status,
			'payment_status'=>$payment_status,
			'warehouse_id'=>$warehouse_id,
			'shipping_method'=>$shipping_method,
			'orderType'=>$orders_print_type,
			'customer_id'=>$customerId,
			'dateType'=>$dateType,
			'dateFrom'=>$dateFrom,
			'dateTo'=>$dateTo,
			'pickup_user_id'=>$pickusersId
		);
		$count=$this->_s->getByCondition($condition,'count(*)','',0,0);
		$data=array(
			'count'=>0,
			'ask'=>0
			);
		if($count>0){
			$service_product = new Product_Service_Product();
			$Rows=$this->_s->getByCondition($condition,'*','add_time desc',$pageSize,$intpage);
			foreach($Rows as $key => $value){
				$Rows[$key]['orders_status_ch']=$this->_s->getOrderStatus($value['orders_status']);
				$Rows[$key]['process_status_ch']=$this->_s->getProcessStatus($value['process_status']);
				$pickupUser = User_Service_User::getByUserId($value['pickup_user_id']);
				$Rows[$key]['pickup_user'] = $this->lang['null'];
				
				//配货员
				if($pickupUser){
					$Rows[$key]['pickup_user'] = $pickupUser['name'];
				}
				
				//打包员 
				$conditionPacker=array(
						"orders_code"=>$value['orders_code'],
						"osh_type"=>"Process",
						"osh_statu_now"=>4,
						
						);
				$packerName=Order_Service_History::getOperator($conditionPacker);
				$Rows[$key]['packer'] = $packerName;
				
				//出货员
				$conditionShipment=array(
						"orders_code"=>$value['orders_code'],
						"osh_type"=>"Shipping",
						"osh_statu_now"=>5,
						);
				
				$shipmenterName=Order_Service_History::getOperator($conditionShipment);
				$Rows[$key]['shipmenter'] = $shipmenterName;
				

				$orderShip = Order_Service_OrderShip::getLastRowByOrdersCode($value['orders_code']);
				$Rows[$key]['tracking_number'] = $this->lang['null'];
				if($orderShip){
					$Rows[$key]['tracking_number'] = $orderShip['tracking_number'];
				}
// 				print_r($this->lang);exit;
				$pickupmap  = Order_Service_OrderPickupMap::getByfield("orders_code",$value['orders_code']);
				$Rows[$key]['op_code'] = empty($pickupmap)?$this->lang['not_pickup']:$pickupmap[0]['op_code'];//下架单号
				
				//订单行
				$getSKUCountRows=Order_Service_Product::getByfield('orders_code', $value['orders_code']);
				$Rows[$key]['sumSKU']=count($getSKUCountRows);
				
				if($browsetype!='fold'){
					$order_product=Order_Service_Product::getByCondition(array('orders_code'=>$value['orders_code']));
					foreach($order_product as $k => $v){
						$w_product=Warehouse_Service_Products::getByProductId($v['product_id']);
						$order_product[$k]['ws_code']=$w_product['ws_code'];
						$category = $service_product->getProductCategory($v['product_id']);
						$order_product[$k]['category'] = "";
						if($category){
							$order_product[$k]['category'] = $category['poc_value'];							
						}
					}
					$Rows[$key]['order_product']=$order_product;
				}
				$country=Common_Service_Country::getByValue($value['consignee_country_id'],'country_id');
				$Rows[$key]['consignee_country_id']=$country['country_code'];
			}
			$data=array(
				'count'=>$count,
				'result'=>$Rows,
				'ask'=>1
			);
		}
		die(Zend_Json::encode($data));

	}
	//导出csv
	public function downAction(){		
		$orders_codes =$this->_request->getParam('orderids');
// 		print_r($orders_codes);
		$filename = date("Y-m-d");
		header("Content-Disposition: attachment; filename=".$filename.".csv");
		header('Content-Type:APPLICATION/OCTET-STREAM');
		echo Order_Service_Order::csv($orders_codes);
	}

	/**
	*根据订单号返回订单日志
	*Param orders_code
	*retrun json
	*/
	public function statushistoryAction(){
		$intpage=$this->_request->getParam('page',1);
		$pageSize=$this->_request->getParam('pageSize');
		$orders_code=$this->_request->getParam('orders_code','');
		$intpage = ($intpage<1)?1:$intpage; 
		$condition=array(
			'orders_code'=>$orders_code,
		);
		$count=Order_Service_History::getByCondition($condition,'count(*)','',0,0);

		$data=array(
			'count'=>0,
			'ask'=>0
			);
		if($count>0){
			$Rows=Order_Service_History::getByCondition($condition,'*',array('osh_id desc'),$pageSize,$intpage);
			foreach($Rows as $key => $value){
				$Urow=User_Service_User::getByUserId($value['user_id']);
				$Rows[$key]['name']=$Urow['name'];
			}
			$data=array(
				'count'=>$count,
				'result'=>$Rows,
				'ask'=>1
			);
		}
		die(Zend_Json::encode($data));

	}

    /*
     * 问题件
     */
    public function problemAction(){
        $remark=trim($this->_request->getParam('comment'));
        $ordersCode=trim($this->_request->getParam('orders_code',''));

        $result=array(
            'ask'=>0,
            'msg'=>'操作失败!',
        );
        if($remark==''){
            $result['msg']='请填写原因!';
            die(Zend_Json::encode($result));
        }
        if($ordersCode==''){
            $result['msg']='订单号不能为空!';
            die(Zend_Json::encode($result));
        }

        $result=Order_Service_Order::updateOrdersProblem($ordersCode,$remark);
        die(Zend_Json::encode($result));

    }

	/**
	*创建订单日志
	*Param orders_code
	*Param osh_remark
	*retrun json
	*/
	public function addcommentAction(){
		$osh_remark=$this->_request->getParam('comment');
		$orders_code=$this->_request->getParam('orders_code','');
		$session = new Zend_Session_Namespace('userAuth');
		$user_id=$session->userID;
		$data=array(
			'ask'=>0,
			'msg'=>$this->lang['add'].$this->lang['fail'],
		);
		$Rows=array(
			'orders_code'=>$orders_code,
			'osh_type'=>'Revise',
			'user_id'=>$user_id,
			'osh_remark'=>$osh_remark,
			'osh_comments'=>$osh_remark,
		);
		$create=0;
		if($osh_remark!='' && $orders_code!=''){
			$create=Order_Service_History::create($Rows);
			if($create>0){
				$data=array(
					'ask'=>1,
					'msg'=>$this->lang['add'].$this->lang['success'],
				);
			}
		}
		die(Zend_Json::encode($data));

	}
	
	public function createAction(){
		$data=array(
			'ask'=>0,
			'msg'=>$this->lang['add'].$this->lang['fail'],
			'result'=>null,
			'orders_code'=>null,
		);
		die(Zend_Json::encode($data));

	}
	
	/**
	*修改订单产品
	*Param op_id
	*/
	public function updateproductAction(){
		$op_id=$this->_request->getParam('op_id','');
		$pd_name=$this->_request->getParam('pd_name','');
		$product_id=$this->_request->getParam('product_id','');
		$product_quantity=$this->_request->getParam('product_quantity','');
		$orders_code=$this->_request->getParam('orders_code','');
		$comment=$this->_request->getParam('comment','');
		$session = new Zend_Session_Namespace('userAuth');
		$user_id=$session->userID;
		$data=array(
			'ask'=>0,
			'msg'=>$this->lang['operation'].$this->lang['fail'],
		);
		$update=array(
			'op_quantity'=>$product_quantity,
			'op_title'=>$pd_name,
			'product_id'=>$product_id,
		);
		$opinfo=Order_Service_Product::getById($op_id);
		$osh_comments="";//日志
		if($opinfo['product_id']!=$product_id){
			$osh_comments.="product_id=".$opinfo['product_id']."=>".$product_id."|";
		}
		if($opinfo['op_title']!=$pd_name){
			$osh_comments.="op_title=".$opinfo['op_title']."=>".$pd_name."|";
		}
		if($opinfo['op_quantity']!=$product_quantity){
			$osh_comments.="op_quantity=".$opinfo['op_quantity']."=>".$product_quantity."|";
		}
		$createlog=array(
			'orders_code'=>$orders_code,
			'osh_type'=>'Revise',
			'user_id'=>$user_id,
			'osh_comments'=>$osh_comments,
			'osh_remark'=>$comment,
		);
		$rows=False;
		if($osh_comments!=''){
			$rows=Order_Service_Product::update($update,$op_id);
			$create=Order_Service_History::create($createlog);
		}
		if($rows==true){
			$subtotal=$this->_s->updateOrderMoney($orders_code);//更新订单物品总价值
			$data=array(
				'ask'=>1,
				'msg'=>$this->lang['operation'].$this->lang['success'],
			);
		}
		die(Zend_Json::encode($data));

	}
	
	/**
	*修改订单
	*Param orders_id,orders_code,formID,form.value
	*/
	public function modifyAction(){
		$orders_id=$this->_request->getParam('orders_id','');
		$orders_code=$this->_request->getParam('orders_code','');
		$formID=$this->_request->getParam('formID','');//提交表单ID
		$consignee_first_name=$this->_request->getParam('consignee_first_name','');
		$consignee_last_name=$this->_request->getParam('consignee_last_name','');
		$consignee_company=$this->_request->getParam('consignee_company','');
		$consignee_country_id=$this->_request->getParam('consignee_country_id','');
		$consignee_zip=$this->_request->getParam('consignee_zip','');
		$consignee_region=$this->_request->getParam('consignee_region','');
		$consignee_city=$this->_request->getParam('consignee_city','');
		$consignee_address1=$this->_request->getParam('consignee_address1','');
		$consignee_address2=$this->_request->getParam('consignee_address2','');
		$consignee_phone=$this->_request->getParam('consignee_phone','');
		$consignee_email=$this->_request->getParam('consignee_email','');
		$session = new Zend_Session_Namespace('userAuth');
		$user_id=$session->userID;
		$update=array();
		$data=array(
			'ask'=>0,
			'msg'=>$this->lang['operation'].$this->lang['fail'],
		);
		if($formID=='shipForm'){ //修改收件人信息
			$update=array(
				'consignee_first_name'=>$consignee_first_name,
				'consignee_last_name'=>$consignee_last_name,
				'consignee_company'=>$consignee_company,
				'consignee_country_id'=>$consignee_country_id,
				'consignee_zip'=>$consignee_zip,
				'consignee_region'=>$consignee_region,
				'consignee_city'=>$consignee_city,
				'consignee_address1'=>$consignee_address1,
				'consignee_address2'=>$consignee_address2,
				'consignee_phone'=>$consignee_phone,
				'consignee_email'=>$consignee_email,
			);
		}
		$orderinfo=$this->_s->getById($orders_id);
		$osh_comments="";//日志
		if($orderinfo['consignee_first_name']!=$consignee_first_name){
			$osh_comments.="consignee_first_name=".$orderinfo['consignee_first_name']."=>".$consignee_first_name."|";
		}
		if($orderinfo['consignee_last_name']!=$consignee_last_name){
			$osh_comments.="consignee_last_name=".$orderinfo['consignee_last_name']."=>".$consignee_last_name."|";
		}
		if($orderinfo['consignee_company']!=$consignee_company){
			$osh_comments.="consignee_company=".$orderinfo['consignee_company']."=>".$consignee_company."|";
		}
		if($orderinfo['consignee_country_id']!=$consignee_country_id){
			$osh_comments.="consignee_country_id=".$orderinfo['consignee_country_id']."=>".$consignee_country_id."|";
		}
		if($orderinfo['consignee_zip']!=$consignee_zip){
			$osh_comments.="consignee_zip=".$orderinfo['consignee_zip']."=>".$consignee_zip."|";
		}
		if($orderinfo['consignee_region']!=$consignee_region){
			$osh_comments.="consignee_region=".$orderinfo['consignee_region']."=>".$consignee_region."|";
		}
		if($orderinfo['consignee_city']!=$consignee_city){
			$osh_comments.="consignee_city=".$orderinfo['consignee_city']."=>".$consignee_city."|";
		}
		if($orderinfo['consignee_address1']!=$consignee_address1){
			$osh_comments.="consignee_address1=".$orderinfo['consignee_address1']."=>".$consignee_address1."|";
		}
		if($orderinfo['consignee_address2']!=$consignee_address2){
			$osh_comments.="consignee_address2=".$orderinfo['consignee_address2']."=>".$consignee_address2."|";
		}
		if($orderinfo['consignee_phone']!=$consignee_phone){
			$osh_comments.="consignee_phone=".$orderinfo['consignee_phone']."=>".$consignee_phone."|";
		}
		if($orderinfo['consignee_email']!=$consignee_email){
			$osh_comments.="consignee_email=".$orderinfo['consignee_email']."=>".$consignee_email."|";
		}
		$createlog=array(
			'orders_code'=>$orders_code,
			'osh_type'=>'Revise',
			'user_id'=>$user_id,
			'osh_comments'=>$osh_comments,
			'osh_remark'=>'人工修改',
		);
		$rows=False;
		if($osh_comments!=''){
			$rows=$this->_s->update($update,$orders_id);
			$create=Order_Service_History::create($createlog);
		}
		if($rows==true){
			$data=array(
				'ask'=>1,
				'msg'=>$this->lang['operation'].$this->lang['success'],
				'formID'=>$formID,
			);
		}
		die(Zend_Json::encode($data));

	}

	/**
	*返回订单详细
	*getParam orders_code
	*return json
	*/
	public function infoAction(){
		$orders_code=$this->_request->getParam('orders_code');
		$return=array(
			'ask'=>0,
			'result'=>null
		);
		if(!empty($orders_code)){
			$rows=$this->_s->getByOrdersCode($orders_code);
			$rows['orders_status']=$this->_s->getOrderStatus($rows['orders_status']);
			$proRow=Order_Service_Product::getByCondition(array('orders_code'=>$orders_code));
			$subtotal=0;//订单总金额
			if(!empty($proRow)){
				foreach($proRow as $key => $value){
					$product=Product_Service_Product::getByProductId($value['product_id']);
					$proRow[$key]['product_title']=$product['product_title'];
					$Picture=Product_Service_Attached::getProductPicture($value['product_id']);//获取图片
// 					$proRow[$key]['proIamges'] = Product_Service_Receiving::getPicturesPath($value['product_id']);
					$proRow[$key]['product_img']=$Picture['pa_path'];
					$proRow[$key]['subtotal']=$value['op_quantity']*$value['op_unit_price'];//产品总价
					$subtotal+=$proRow[$key]['subtotal'];
				}
			}
			$return=array(
				'ask'=>1,
				'result'=>$rows,
				'productList'=>$proRow,
				'subtotal'=>$subtotal,
			);
		}
		die(Zend_Json::encode($return));
	}
	
	/**
	*删除订单
	*Param ws_id
	*/
	public function deleteAction(){
    	set_time_limit(0);
		$rows=array("ask"=>0,"msg"=>"orders_code empty");
		$session=new Zend_Session_Namespace('userAuth');
		
		$allowUserIds = array(
				1,//admin
				);
		if(!in_array($session->userID, $allowUserIds)){
			$rows['msg'] = "no permission";
			die(Zend_Json::encode($rows));
		}
		$order_code=$this->_request->getParam('order_code','');
		
		if(!empty($order_code)){
			if(is_array($order_code)){
				$rows['msg']="";
				foreach($order_code as $v){
					$service_orderProcess = new Order_Service_OrderProccess($v);
					$row=$service_orderProcess->delete();
					$rows['ask'] = 1;
					$rows['msg'].= "OrderCode：".$v.":".$row['msg']."<br/>";
				}
			}elseif(is_string($order_code)){
				$service_orderProcess = new Order_Service_OrderProccess($order_code);
				$rows=$service_orderProcess->delete();
				$rows['msg'].= "Order Code：".$order_code.":".$rows['msg']."";
			}
			
		}
		
		die(Zend_Json::encode($rows));
	}
	
	/**
	*删除订单产品
	*Param op_id
	*/
	public function deleteproductAction(){
		$op_id=$this->_request->getParam('op_id','');
		$orders_code=$this->_request->getParam('orders_code','');
		$osh_remark=$this->_request->getParam('comment','');
		$session = new Zend_Session_Namespace('userAuth');
		$user_id=$session->userID;
		$rows=0;
		$data=array(
			'ask'=>0,
			'msg'=>$this->lang['operation'].$this->lang['fail'],
		);
		if($op_id!='' && $osh_remark!='' && $orders_code!=''){
			$opRows=Order_Service_Product::getById($op_id);
			$osh_comments="deleted product_id:".$opRows['product_id']."|op_id:".$op_id;
			$Rows=array(
				'orders_code'=>$orders_code,
				'osh_type'=>'Revise',
				'user_id'=>$user_id,
				'osh_remark'=>$osh_remark,
				'osh_comments'=>$osh_comments,
			);
			$create=Order_Service_History::create($Rows);
			if($create>0){
				$rows=Order_Service_Product::delete($op_id);
			}
		}
		if($rows==True){
			$subtotal=$this->_s->updateOrderMoney($orders_code);//更新订单物品总价值
			$data=array(
				'ask'=>1,
				'msg'=>$this->lang['operation'].$this->lang['success'],
				'op_id'=>$op_id,
				'order_subtotal'=>$subtotal,
			);
		}

		die(Zend_Json::encode($data));
	}
	
	/**
	*添加订单产品
	*Param product_id
	*/
	public function addproductAction(){
		$product_id=$this->_request->getParam('product_id','');
		$orders_code=$this->_request->getParam('orders_code','');
		$session = new Zend_Session_Namespace('userAuth');
		$user_id=$session->userID;
		$rows=0;
		$data=array(
			'ask'=>0,
			'msg'=>$this->lang['operation'].$this->lang['fail'],
		);
		if($product_id!='' && $orders_code!=''){
			$condition=array(
				'product_id'=>$product_id,
				'orders_code'=>$orders_code,
			);
			$opRows=Order_Service_Product::getByCondition($condition,'count(*)','',0,0);
			if($opRows==false){//未存在
				$product=Product_Service_Product::getByProductId($product_id);
				$addproduct=array(
					'product_id'=>$product_id,
					'orders_code'=>$orders_code,
					'product_sku'=>$product['product_sku'],
					'op_title'=>$product['product_title'],
					'op_quantity'=>1,
					'op_weight'=>$product['product_weight'],
					'op_length'=>$product['product_length'],
					'op_width'=>$product['product_width'],
					'op_height'=>$product['product_height'],
					'op_declared_value'=>$product['product_declared_value'],
				);
				$addRow=Order_Service_Product::create($addproduct);
						$osh_comments="add orders_product product_id:".$product_id;
				$Rows=array(
					'orders_code'=>$orders_code,
					'osh_type'=>'Revise',
					'user_id'=>$user_id,
					'osh_remark'=>'create',
					'osh_comments'=>$osh_comments,
				);
				$create=Order_Service_History::create($Rows);
				if($addRow==True && $create==True){
					$subtotal=$this->_s->updateOrderMoney($orders_code);//更新订单物品总价值
					$data=array(
						'ask'=>1,
						'msg'=>$this->lang['operation'].$this->lang['success'],
						'order_subtotal'=>$subtotal,
					);
				}
			}else{
					$data=array(
						'ask'=>0,
						'msg'=>$this->lang['order_contain_this_product_change_some_prop'],
					);
			}
		}
		die(Zend_Json::encode($data));
	}
	
	
	public function printAction(){

		
	}
	
	public function printPopAction(){
		$code=trim($this->_request->getParam('code',''));
		if($code){
			$ordersCode=trim($this->_request->getParam('ordersCode',''));
			$js=trim($this->_request->getParam('js','yes'));
			$reprint=trim($this->_request->getParam('reprint',''));
				
			$order=$this->_s->getByOrdersCode($ordersCode);
            if(empty($order)){
                echo 'no result';
                exit;
            }

			if($js=="yes"){
                $js=1;
			}
            $order['country']=Common_Service_Country::getByValue($order['consignee_country_id']);
            $this->view->close=$js;
            $this->view->order=$order;

            $tpl='';
            $sm=strtoupper($order['shipping_method']);
            switch($sm){
                case 'HKBAM':
                    $tpl='HKBAM';
                    break;
                default:
                    $tpl='RM1';
                    break;
            }


            $html=$this->view->render($this->tplDirectory.$tpl.'.tpl');
			//重新打印
			if($reprint){
				echo $html;exit;
			}
				
			//打包贴标
			$result = Order_Service_OrderProccess::packageAndLabel($ordersCode);
			//$result['ask']=1;

			if($result['ask']==1){
				echo $result['ask'].$html;
			}else{
				echo $result['ask'].$result['msg'];
			}		
			exit;
		}
		echo Cff::renderTpl($this->tplDirectory.'print.tpl',"layout");
	}
	
	public function orderCodeIframeAction(){

	}
	
	public function stopAction(){	
		
		$this->view->stopStatus = array("1"=>$this->lang['waiting_for_stop'],"2"=>$this->lang['stoped']);
		$value = '1';
		$field = 'st_status';
		$shipTypeArr = Shipping_Service_ShipType::getAllByValue ( $value, $field );
		$this->view->shipTypeArr = $shipTypeArr;
		
		echo Cff::renderTpl($this->tplDirectory.'stop.tpl',"layout");
	}
	
	
	public function draftStopAction(){
		$code = trim($this->_request->getParam('code',''));	//搜索表单提交的订单代码
		$stoporderid = trim($this->_request->getParam('stoporderid',''));
		if($code){
			$order = $this->_s->getByOrdersCode($code);
			//可能是物流单
			if(!$order){
				$ordertemp=Order_Service_OrderShip::getByfield('tracking_number', $code);
					
				if($ordertemp){
					if($ordertemp[0]['orders_code']){
						$orders_shipment=$ordertemp[0]['orders_code'];
						$order = Order_Service_Order::getByOrdersCode ( $orders_shipment );
					}
				}
			}
		}
		
		if($stoporderid){
			$order_temp = $this->_s->getByfield('order_id', $stoporderid);
			$order = $this->_s->getByOrdersCode($order_temp[0]['orders_code']);

		}
		
		if(!$order){
			$data=array(
					'result'=>$code." ".$this->lang['order'].$this->lang['not_exists'],
					'ask'=>0
			);
			die(Zend_Json::encode($data));
		}
		
		
		
		if($order['underreview_status']==3){
			
			if($order['orders_status']==0 && $order['process_status']==0){
				$data=array(
							'result'=>$this->lang['order_has_stoped'],
							'ask'=>0
					);
					die ( Zend_Json::encode ( $data ) );
					exit;
			}
			
				
				$row_opm_data=array();
					
				//根据订单号找配货表
				$row_opm=array();
				$row_opm=Order_Service_OrderPickupMap::getByfield('orders_code', $order['orders_code']);
				//上面这个方法取不到值要重写一下
				
				if($row_opm){
					foreach ($row_opm as $val){
						$row_wp['product_id']=$val['product_id'];
						$row_wpp=Product_Service_Product::getByProductId($val['product_id']);
						$row_wp['product_sku']=$row_wpp['product_sku'];
						$row_wp['product_title']=$row_wpp['product_title'];
						$row_wp['opm_quantity']=$val['opm_quantity'];
						$row_wp['wpb_id']=$val['wpb_id'];
						$row_wpb=Warehouse_Service_ProductsBatch::getByfield('wpb_id', $row_wp['wpb_id']);
						$row_wp['ws_code']=$row_wpb['ws_code'];
						$row_wp['warehouse_id']=$row_wpb['warehouse_id'];
						
						//查看是否有相关的货架正在盘点
						$tempCondCheckWP=array(
								'warehouse_id'=>$row_wp['warehouse_id'],
								'product_id'=>$row_wp['product_id'],
								'ws_code'=>$row_wp['ws_code']
								);
						$rowResult=Warehouse_Service_Products::getByCondition($tempCondCheckWP);
						if($rowResult['wp_status']==2){
							$data=array(
									'result'=>$this->lang['order_ws_code_pandian'].'-->'.$row_wp['ws_code'].'',
									'ask'=>0
							);
							die ( Zend_Json::encode ( $data ) );
						}
						
						//$row_wp['customer_id']=$this->_customerId;
						$row_opm_data[]=$row_wp;
					}
				}
				
				$result["orders"] = $order;
				
				$order['date']=date('Y-m-d',time());
				$result["product"] = $row_opm_data;

				$this->view->order = $order;
				$this->view->result = $result;
				
                $html=$this->view->render($this->tplDirectory."stop-print-detail.tpl");
                //$data=array("html"=>$html,'ask'=>1);
               
                $stopaction=new Order_Service_OrderProccess($order['orders_code']);
                $result=$stopaction->stop();
                
                if($result['ask']==1){
                	$data=array(
                			'result'=>$code." ".$this->lang['operation'].$this->lang['success'],
                			"html"=>$html,
                			'ask'=>1
                	);
                }else{
                	$data=array(
                			'result'=>$order['orders_code']."|".$result['msg'],
                			'ask'=>0
                	);
                }

                die ( Zend_Json::encode ( $data ) );
				exit;
			}else{
				$data=array(
						'result'=>$code." ".$this->lang['order_not_waiting_for_stop'],
						'ask'=>0
				);
				die(Zend_Json::encode($data));
			}		
		die(Zend_Json::encode($data));
	}
	
	public function confirmStopAction(){
		$data=array(
				'count'=>0,
				'ask'=>0
		);
		die(Zend_Json::encode($data));
	}
	
	
	public function printStopAction(){
		$data=array(
				'count'=>0,
				'ask'=>0
		);
		die(Zend_Json::encode($data));
	}
	
	public function stopListAction(){
		$intpage=$this->_request->getParam('page',1);
		$pageSize=$this->_request->getParam('pageSize');
		
		$shipping_method = $this->_request->getParam ( 'st_name', '' );
		
		//$status = $this->_request->getParam('underreview_status','3');//代码类型
		$code = trim($this->_request->getParam('code',''));	//搜索表单提交的订单代码
		
		$dateFrom = trim ( $this->_request->getParam ( "dateFrom", "" ) );
		$dateTo = trim ( $this->_request->getParam ( "dateTo", "" ) );
		
		$status=$this->_request->getParam('orders_status_sch','1');
		
		$condition=array(
				'orders_code'=>$code,
				'shipping_method' => $shipping_method,
				'underreview_status'=>3
		);
	
		$intpage = ($intpage<1)?1:$intpage;
		if($status==1){
			$condition=array(
					'orders_code'=>$code,
					"stop_date_from" => $dateFrom,
					"stop_date_to" => $dateTo,
					'shipping_method' => $shipping_method,
					'underreview_status'=>3
			);
		}else if($status==2){
			$condition=array(
					'orders_code'=>$code,
					"stop_date_from" => $dateFrom,
					"stop_date_to" => $dateTo,
					'orders_status'=>'0',
					'process_status'=>'0',
					'shipping_method' => $shipping_method,
					'underreview_status'=>3
			);
		}
		
		$count=$this->_s->getStopByCondition($condition,'count(*)','',0,0);
		$data=array(
				'count'=>0,
				'ask'=>0
		);
		if($count>0){
			$service_product = new Product_Service_Product();
			$Rows=$this->_s->getStopByCondition($condition,'*','order_stop_date desc',$pageSize,$intpage);
			foreach($Rows as $key => $value){
				$Rows[$key]['orders_status']=$this->_s->getOrderStatus($value['orders_status']);
				$Rows[$key]['process_status_ch']=$this->_s->getProcessStatus($value['process_status']);
				
				$country=Common_Service_Country::getByValue($value['consignee_country_id'],'country_id');
				$Rows[$key]['consignee_country_id']=$country['country_code'];
			}
			$data=array(
					'count'=>$count,
					'result'=>$Rows,
					'ask'=>1
			);
		}
		//		print_r($data);exit;
		die(Zend_Json::encode($data));
	
	}
	/**
	 *订单产品
	 *Param orders_code
	 */
	public function orderProductAction(){
		$orders_code=$this->_request->getParam('orders_code','');
		$rows=Order_Service_Product::getByfield("orders_code", $orders_code);
		$data = array("ask"=>0);
		if($rows){
			$data['ask'] = 1;
			foreach($rows as $k=>$v){
				$rows[$k]['pic'] = "/product/product/view-picture/id/".($v["product_id"]);
				
				$product = Product_Service_Product::getByProductId($v["product_id"]);
				$rows[$k]['product'] =$product;
				$rows[$k]['title'] =$product['product_title'];
				$rows[$k]['customer_id'] =$product['customer_id'];
				$rows[$k]['title_cn'] =$product['product_title_cn'];
			}
			$data['row'] = $rows;
		}
// 		print_r($date);
		die(Zend_Json::encode($data));
	}
	
	
	/**
	*
	*异常订单定时处理
	*/
	public function orderResubmitAction(){		
		$result = Order_Service_OrderProccess::orderResubmit();
		print_r($result);
		exit;
	}
	
}